package com.example.springbootrabbitmq.demo4exchangedirect;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * <p><em>Created on 2022/8/27 9:06 下午</em></p>
 *
 * Direct和Fanout很相似,
 * 但是唯一不同的就是每一个Queue都与Exchange设置一个BindingKey,
 * 而且在发布消息的时候也需要指定消息的RoutingKey
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
@Component
public class DirectReceiver {

    @RabbitListener(bindings = @QueueBinding(
            //队列名称
            value = @Queue("direct.queue1"),
            //交换机名称
            exchange = @Exchange(value = "directExchange", type = ExchangeTypes.DIRECT),
            //key
            key = {"direct.message", "direct.message2"}
    ))
    public void listenDirectQueue1(String msg){
        System.out.println("消费者接收到direct.queue1的消息：【" + msg + "】");
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue2"),
            exchange = @Exchange(value = "directExchange"),
            key = {"direct.message", "direct.message3"}
    ))
    public void listenDirectQueue2(String msg){
        System.out.println("消费者接收到direct.queue2的消息：【" + msg + "】");
    }

}
